import { defineStore } from 'pinia'
import { ref } from 'vue'
import { userGetInfoService } from '@/api/user'
//用户模块 token setToken removeToken
export const useUserStore = defineStore(
  'big-user',
  () => {
    const token = ref('') //自定义token
    const setToken = (newToken) => {
      //修改token方法
      token.value = newToken
    }
    const removeToken = () => {
      token.value = ''
    }
    const user = ref({}) //自定义User对象
    const getUser = async () => {
      const res = await userGetInfoService() // 请求获取数据
      user.value = res.data.data // 修改user
    }
    const setUser = async (newUser) => {
      user.value = newUser // 修改user
    }
    return {
      //导出
      token,
      setToken,
      removeToken,
      user,
      getUser,
      setUser
    }
  },
  {
    persist: true // 持久化
  }
)
